// pages/my/my.js
const app = getApp();
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		bgsrc: '',
		headicon: '../../images/moren.png',
		nickname: '请登录',
		dayu: '../../images/dayuhao.png',
		fulllists: [
			{ id: 1, src: '../../images/daifukuan.png', text: '待付款' },
			{ id: 2, src: '../../images/daiwancheng.png', text: '待发货' },
			{ id: 3, src: '../../images/yiwancheng.png', text: '待收货' },
			{ id: 4, src: '../../images/daipingjia.png', text: '待评价' },
		],
		modulelists: [
			{ url: '../myYouhui/myYouhui?id=1', src: '../../images/wodeyouhuiquan.png', text: '我的优惠券' },
			{ url: '../toRecharge/toRecharge', src: '../../images/wodejifen.png', text: '我的余额' },
			{ url: '', src: '../../images/wodedizhi.png', text: '我的地址' },
		],
		disabled: false,
	},
	login(userinfo) {
		var that = this;
		app.login(userinfo, (err, res) => {
			if (err) { return console.log('login function has error') }
			else {
				that.setData({
					nickname: app.globalData.user.nickname,
					headicon: app.globalData.user.avatar_url,
					bgsrc: app.globalData.user.avatar_url,
					disabled: true
				})
			}
		})
	},
	navgoto: function (e) {
		if (e.currentTarget.dataset.url == '') {
			var that = this;
			wx.chooseAddress({
				success: function (res) {
				},
				fail: function (res) {
					wx.getSetting({
						success(res) {
							if (!res.authSetting['scope.address']) {
								wx.authorize({
									scope: 'scope.address',
									success(res) {
									},
									fail: function (res) {
										wx.openSetting({
											success: (res) => {
												if (!res.authSetting["scope.address"]) {
													wx.showModal({
														title: '提示',
														content: '通讯地址授权失败，不能使用外卖',
														showCancel: false,
														success: function (res) {
														}
													})
												}
											}
										})
									}
								})
							}
						}
					})
				}
			})
			return false;
		}
		wx.navigateTo({
			url: e.currentTarget.dataset.url,
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		var that = this;
		// if (app.globalData.user.nickname){
		//   that.setData({
		//     nickname: app.globalData.user.nickname,
		//     headicon: app.globalData.user.avatar_url,
		//     bgsrc: app.globalData.user.avatar_url,
		//     disabled:true
		//   })
		// }
		wx.request({
			url: app.globalData.directory + 'api/v2/getUserAllInfo',
			header: {
				'content-type': 'application/json',
				'token': wx.getStorageSync('token')
			},
			success: function (res) {
				if (res.data.status == 'ok') {
					// console.log(res.data.data.user_level.level_name,2222)
					that.setData({
						user_level: res.data.data.user_level.level_name,
						level_id: res.data.data.user_level.id,
						nickname: res.data.data.nickname,
						headicon: res.data.data.avatar_url,
						bgsrc: res.data.data.avatar_url,
					})
				} else {
					wx.showToast({
						title: '请求失败,请重试',
						icon: 'none',
						duration: 2000,
					})
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})